﻿using Castle.Core.Resource;
using Microsoft.EntityFrameworkCore;
using SeckillService.Data;
using SeckillService.Model;
using SeckillService.Services.Dtos;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace SeckillService.Repositorys
{
    [Dependency(ServiceLifetime.Transient)]
    public class SeckillRepository : EfCoreRepository<SeckillServiceDbContext, Seckill, Guid>, ISeckillRepository
    {
        public SeckillRepository(IDbContextProvider<SeckillServiceDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }

        public override Task<List<Seckill>> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default)
        {
            return base.GetListAsync(includeDetails, cancellationToken);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="SeckillProductId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<SeckillProduct> GetSeckillProductAsync(Guid SeckillProductId)
        {
            DbSet<Seckill> Seckills = await GetDbSetAsync();
            var dt = Seckills.Include(x => x.SeckillProducts)
                .SelectMany(x => x.SeckillProducts)
                .Where((x) => x.Id == SeckillProductId)
                .First();
            return dt;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="SeckillProductId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<SeckillProduct> GetSeckillProductByProductIdAsync(Guid SeckillProductId)
        {
            DbSet<Seckill> Seckills = await GetDbSetAsync();
            var seckill = await Seckills.Include(x => x.SeckillProducts)
                .SelectMany(x => x.SeckillProducts)
                .Where(x => x.Id == SeckillProductId).FirstAsync();
            return seckill;
        }

        /// <summary>
        /// 获取秒杀编号下商品列表
        /// </summary>
        /// <param name="SeckillId"></param>
        /// <returns></returns>
        public async Task<List<SeckillProduct>> GetSeckillProductsAsync(Guid SeckillId)
        {
            DbSet<Seckill> Seckills = await GetDbSetAsync();
            var seckill = await Seckills.Include(x => x.SeckillProducts)
                .Where(x => x.Id == SeckillId).FirstAsync();
            return seckill.SeckillProducts;
        }

        /// <summary>
        /// 更新库存
        /// </summary>
        /// <param name="updateSeckillProductStockDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task UpdateSeckillProductStockAsync(SeckillProduct seckillProduct)
        {
            SeckillServiceDbContext dbContext = await GetDbContextAsync();
            dbContext.Attach(seckillProduct);
            dbContext.Entry(seckillProduct).Property(x => x.SeckillStock).IsModified = true;
        }
    }
}
